<template>
  <div>

    <tableComponents
      :isShow="false"
      @toAdd='addImg'
      :isHaveExport="false"
      :isHavePagination="false"
    >
      <template v-slot:table>
        <el-table
          stripe
          v-loading="loading"
          header-row-class-name="head_color"
          :data="list.data"
          tooltip-effect="dark"
          style="width: 99.8%"
          :default-sort="{prop: 'sort'}"
        >
          <el-table-column header-align="left" align="center" min-width="300" label="广告地址">
            <template slot-scope="scope">
              <div class="advert_table">
                <div @click="imgShow(scope.row.piclink1)">
                  <img :src="scope.row.piclink1" alt />
                  <p>IOS1125*2436</p>
                </div>
                <div @click="imgShow(scope.row.piclink2)">
                  <img :src="scope.row.piclink2" alt />
                  <p>IOS1242*2208</p>
                </div>
                <div @click="imgShow(scope.row.piclink3)">
                  <img :src="scope.row.piclink3" alt />
                  <p>IOS750*1334</p>
                </div>
                <div @click="imgShow(scope.row.piclink4)">
                  <img :src="scope.row.piclink4" alt />
                  <p>IOS640*960</p>
                </div>
                <div @click="imgShow(scope.row.piclink5)">
                  <img :src="scope.row.piclink5" alt />
                  <p>安卓720*1242</p>
                </div>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="操作" min-width="120">
            <template slot-scope="scope">
              <el-button
                :loading="$store.state.isLoading"
                type="primary"
                size="mini"
                @click="updateImg(scope.row.id)"
              >编辑</el-button>
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                type="danger"
                @click="deleteImg(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </tableComponents>

    <el-dialog
      :close-on-click-modal="false"
      title="启动页图片设置"
      width="800px" class="tl size_13"
      @close="closeDio"
      :visible.sync="dialogImg"
    >
      <el-form :model="form" ref="form" label-width="130px">
        <el-form-item label="IOS--1125*2436">
          <el-col :span="20" class="tl">
            <el-col :span="12">
              <el-input v-model="form.piclink1" autocomplete="off" clearable class="mar_bottom_5"></el-input>
            </el-col>
            <el-col :span="3">
              <el-button
                :loading="$store.state.isLoading"
                type="success"
                size="small "
                class="is_mar"
                @click="imgShow(form.piclink1)"
              >查看</el-button>
            </el-col>
            <el-col :span="4">
              <compressimgUpload
                :skuId="'piclink1'"
                class="uploadIsXuan"
                :isGzip="false"
                listType="text"
                :isCropper="false"
                :onSuccess="onSuccess"
                :isOneImg="true"
                :files="fileList"
              ></compressimgUpload>
            </el-col>
          </el-col>
        </el-form-item>

        <el-form-item label="IOS--1242*2208">
          <el-col :span="20" class="tl">
            <el-col :span="12">
              <el-input v-model="form.piclink2" autocomplete="off" clearable class="mar_bottom_5"></el-input>
            </el-col>
            <el-col :span="3">
              <el-button
                :loading="$store.state.isLoading"
                type="success"
                size="small "
                class="is_mar"
                @click="imgShow(form.piclink2)"
              >查看</el-button>
            </el-col>
            <el-col :span="4">
              <compressimgUpload
                :skuId="'piclink2'"
                class="uploadIsXuan"
                :isGzip="false"
                listType="text"
                :isCropper="false"
                :onSuccess="onSuccess"
                :isOneImg="true"
                :files="fileList"
              ></compressimgUpload>
            </el-col>
          </el-col>
        </el-form-item>

        <el-form-item label="IOS--750*1334">
          <el-col :span="20" class="tl">
            <el-col :span="12">
              <el-input v-model="form.piclink3" autocomplete="off" clearable class="mar_bottom_5"></el-input>
            </el-col>
            <el-col :span="3">
              <el-button
                :loading="$store.state.isLoading"
                type="success"
                size="small "
                class="is_mar"
                @click="imgShow(form.piclink3)"
              >查看</el-button>
            </el-col>
            <el-col :span="4">
              <compressimgUpload
                :skuId="'piclink3'"
                class="uploadIsXuan"
                :isGzip="false"
                listType="text"
                :isCropper="false"
                :onSuccess="onSuccess"
                :isOneImg="true"
                :files="fileList"
              ></compressimgUpload>
            </el-col>
          </el-col>
        </el-form-item>

        <el-form-item label="IOS--640*960">
          <el-col :span="20" class="tl">
            <el-col :span="12">
              <el-input v-model="form.piclink4" autocomplete="off" clearable class="mar_bottom_5"></el-input>
            </el-col>
            <el-col :span="3">
              <el-button
                :loading="$store.state.isLoading"
                type="success"
                size="small "
                class="is_mar"
                @click="imgShow(form.piclink4)"
              >查看</el-button>
            </el-col>
            <el-col :span="4">
              <compressimgUpload
                :skuId="'piclink4'"
                class="uploadIsXuan"
                :isGzip="false"
                listType="text"
                :isCropper="false"
                :onSuccess="onSuccess"
                :isOneImg="true"
                :files="fileList"
              ></compressimgUpload>
            </el-col>
          </el-col>
        </el-form-item>

        <el-form-item label="安卓--720*1242">
          <el-col :span="20" class="tl">
            <el-col :span="12">
              <el-input v-model="form.piclink5" autocomplete="off" clearable class="mar_bottom_5"></el-input>
            </el-col>
            <el-col :span="3">
              <el-button
                :loading="$store.state.isLoading"
                type="success"
                size="small "
                class="is_mar"
                @click="imgShow(form.piclink5)"
              >查看</el-button>
            </el-col>
            <el-col :span="4">
              <compressimgUpload
                :skuId="'piclink5'"
                class="uploadIsXuan"
                :isGzip="false"
                listType="text"
                :isCropper="false"
                :onSuccess="onSuccess"
                :isOneImg="true"
                :files="fileList"
              ></compressimgUpload>
            </el-col>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button
            :loading="$store.state.isLoading"
            type="primary"
            @click="saveImg"
            size="mini"
          >立即提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="showImg" :append-to-body="true" width="400px">
      <img width="100%" :src="$fnc.getImgUrl(img_link)" alt />
    </el-dialog>
  </div>
</template>
<script>
import HeadTitle from "@/components/currency/HeadTitle.vue";
import uploadpic from "@/components/page/pic/up_pic";
import compressimgUpload from "@/components/currency/compressimgUpload.vue";
export default {
  data() {
    return {
      loading: false,
      dialogImg: false,
      showImg: false,
      img_link: "",
      list: {
        data: {}
      },
      form: {
        piclink1: "",
        piclink2: "",
        piclink3: "",
        piclink4: "",
        piclink5: ""
      },
      fileList: []
    };
  },
  components: {
    HeadTitle,
    uploadpic,
    compressimgUpload
  },
  created() {
    this.getAdvertList();
  },
  methods: {
    imgShow(img) {
      this.img_link = img;
      this.showImg = true;
    },
    getAdvertList() {
      this.$api.getPage.get_advert_lists({}).then(res => {
        if (res.data.code == 200 && res.data.status == 1) {
          this.list.data = res.data.result;
        }
      });
    },
    onSuccess(pic, index) {
      this.$set(this.form, index, pic);
    },
    addImg() {
      this.form = {};
      this.dialogImg = true;
    },
    saveImg() {
      if (this.form.id) {
        this.$api.getPage.advert_saveedit(this.form).then(res => {
          if (res.data.code == 200 && res.data.status == 1) {
            this.$fnc.alertSuccess({ text: "修改成功" });
            this.dialogImg = false;
            this.getAdvertList();
          } else {
            this.$fnc.alertError(res.data.result);
          }
        });
      } else {
        this.$api.getPage.advert_saveadd(this.form).then(res => {
          if (res.data.code == 200 && res.data.status == 1) {
            this.$fnc.alertSuccess({ text: "添加成功" });
            this.dialogImg = false;
            this.getAdvertList();
          } else {
            this.$fnc.alertError(res.data.result);
          }
        });
      }
    },
    updateImg(id) {
      this.$api.getPage.advert_edit({ id: id }).then(res => {
        if (res.data.code == 200 && res.data.status == 1) {
          this.form = res.data.result;
          this.dialogImg = true;
        }
      });
    },
    deleteImg(id) {
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          var data = { id: id };
          this.$api.getPage.advert_del(data).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.getAdvertList();
            } else {
              this.$swal("删除失败！", "出错啦！", "success");
            }
          });
        }
      });
    },
    closeDio() {
      this.dialogImg = false;
    }
  },
  computed: {},
  watch: {}
};
</script>
<style lang="less" scoped>
.advert_table {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  > div {
    width: 20%;
    margin-right: 10px;
    img > {
      width: 50%;
      vertical-align: middle;
    }
    > p {
      padding-top: 8px;
    }
  }
}
</style>
